import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class CSVIO {
	final static String PATH = "";
	/**
	 * given a csv file name, return the contents in a 2d arraylist
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static ArrayList<ArrayList<String>> readFile(String filename)
			throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(PATH+filename));
		ArrayList<ArrayList<String>> my2DArrayList = new ArrayList<ArrayList<String>>();
		String line = null;
		int outer_counter = 0;
		while ((line = in.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(line, ",");
			my2DArrayList.add(new ArrayList<String>());
			while (st.hasMoreElements()) {
				// add elements to the row and remove all quotation marks if
				// they exist
				my2DArrayList.get(outer_counter).add(
						st.nextToken().replaceAll("\"", ""));
				// System.out.print(st.nextToken() + " ");
			}
			outer_counter++;

		}
		return my2DArrayList;

	}
/**
 * given a csv string, read it and return it in a 2d arraylist
 * @param stringname
 * @return
 * @throws IOException
 */
	public static ArrayList<ArrayList<String>> readString(String stringname)
			throws IOException {

		ArrayList<ArrayList<String>> my2DArrayList = new ArrayList<ArrayList<String>>();
		StringTokenizer st_outer = new StringTokenizer(stringname, "\n");
		int counter = 0;
		while (st_outer.hasMoreTokens()) {
			String line = st_outer.nextToken();
			StringTokenizer st_inner = new StringTokenizer(line, ",");
			my2DArrayList.add(new ArrayList<String>());
			while (st_inner.hasMoreElements()) {
				// add elements to the row and remove all quotation marks if
				// they exist
				my2DArrayList.get(counter).add(
						st_inner.nextToken().replaceAll("\"", ""));
			}
			counter++;
		}

		return my2DArrayList;

	}
/**
 * write the csv file, given a 2d arraylist
 * @param filename
 * @param my2DArrayList
 * @throws IOException
 */
	public static void writeFile(String filename,
			ArrayList<ArrayList<String>> my2DArrayList) throws IOException {
		SystemSafety.turnOn();
		BufferedWriter out = new BufferedWriter(new FileWriter(PATH+filename));
		for (int r = 0; r < my2DArrayList.size(); r++) {
			for (int c = 0; c < my2DArrayList.get(r).size(); c++) {
				out.write(my2DArrayList.get(r).get(c) + ",");
				// System.out.print(r
				// +"."+c+"-"+my2DArrayList.get(r).get(c)+",");
			}
			out.newLine();
			// System.out.println();
		}
		out.flush();
		out.close();
		SystemSafety.turnOff();

	}
/**
 * return true if the specified file contains the specified word
 * @param csvFileName
 * @param word
 * @return
 * @throws IOException
 */
	public static boolean contains(String csvFileName, String word)
			throws IOException {
		ArrayList<ArrayList<String>> my2DArrayList = readFile(csvFileName);
		for (ArrayList<String> row : my2DArrayList) {
			for (String column : row) {
				if (argumentize(column).equals(argumentize(word))) {
					return true;
				}
			}
		}
		return false;
	}
	/**
	 * remove all nonalphanumeric characters and transformt o lowercase
	 * @param incoming
	 * @return
	 */
	public static String argumentize(String incoming) {
		return incoming.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
	}
	/**
	 * add a line to an existing file.
	 * @param csvFileName
	 * @param line
	 * @throws IOException
	 */
	public static void append(String csvFileName, String line)
			throws IOException {
		ArrayList<ArrayList<String>> my2dlist = readFile(csvFileName);
		StringTokenizer st = new StringTokenizer(line,",");
		ArrayList<String> toappend= new ArrayList<String>();
		while(st.hasMoreTokens()){
			toappend.add(st.nextToken());
		}
		writeFile(csvFileName,my2dlist);
/*		SystemSafety.turnOn();
		PrintWriter pw = new PrintWriter(new FileWriter(PATH+csvFileName, true));
		pw.println(line);
		pw.close();
		SystemSafety.turnOff();
*/	}


	/**
	 * Given a csv filename, return the element located in specified row and column
	 * @param filename
	 * @param row
	 * @param column
	 * @return
	 * @throws IOException
	 */
	public static String getValue(String filename, int row,int column) throws IOException{
		ArrayList<ArrayList<String>> my2DArrayList = readFile(filename);
		return my2DArrayList.get(row).get(column);
		
	}


	/**
	 * TEST method. simply to test the functionality of csvio or driver
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String args[]) throws IOException {
		ArrayList<ArrayList<String>> my2DArrayList1;
		my2DArrayList1 = readFile("1.csv");
		writeFile("test4.csv", my2DArrayList1);
		System.out.println("arg0:" + args[0] + "  arg1:" + args[1]);

	}

}
